#### Conjoint Analysis ####

# Encoding: UTF-8

# Set session to folder with the replication materials
setwd("~")

# -------------------- 1. Libraries and functions --------------------

pacman::p_load(cregg, dplyr, statar, ggplot2, tidyr, ggpubr, countrycode)

source("1. Functions.R")

# --------------------------- 2. Data ---------------------------

data <- readRDS("data_ISP.rds")

# --------------------- 3. Objects for Plots --------------------

path_plots <- "./Plots/"
data_date <- "2023_11_30_"
type <- ".png"

# --------------------- 4. No subset (Fig. 2) ---------------------

plot_no_subset(data, height = 10, width = 20)

# ---------------------------- 5. Country or Region of PhD Studies (Fig 4.) ---------------------------


cntrs_glbl.nrth <- c("Estados Unidos", "Alemania", "Australia", "Canada","Belgium", "España",
                     "Dinamarca", "Francia","Israel", "Italia", "Japón","Países Bajos",
                     "Portugal", "Reino Unido", "Suiza", "Polonia") 
cntrs_glbl.sth <- c("China","Rusia","Sudáfrica","Turquía") 
data$cntry.stdy_glbl.nrth.tri <- factor(ifelse(data$`2.6` %in%
                                                 cntrs_glbl.nrth,
                                               "Global North", 
                                               ifelse(data$`2.6` %in%
                                                        cntrs_glbl.sth,"Global South","Latin America")))

data%>%
  tab(cntry.stdy_glbl.nrth.tri)

# Fig 19
data_temp <- data %>%
  filter(cntry.stdy_glbl.nrth.tri == "Global North" | cntry.stdy_glbl.nrth.tri == "Latin America") %>%
  rename(stdy.region = "cntry.stdy_glbl.nrth.tri") %>%
  mutate(stdy.region = factor(stdy.region))

data_temp <- data %>%
  filter(`2.6`== "Estados Unidos" | cntry.stdy_glbl.nrth.tri == "Latin America") %>%
  mutate(usa.la = ifelse(`2.6`=="Estados Unidos", "United States","Latin America"))%>%
  mutate(usa.la=factor(usa.la))


plot_subset(data=data_temp, by = ~ usa.la, by_name = "", out=c(),
            title = "Subsets According to Country where the Maximum Level of Study was Reached",
            filename = "03_cntry.stdy_us.lam")



# Fig 20

cntrs_euro <- c("Alemania", "Belgium", "España",
                "Dinamarca", "Francia", "Italia", "Países Bajos",
                "Portugal", "Reino Unido", "Suiza", "Polonia") 
cntrs_nam<-c("Estados Unidos")
data$cntry.stdy_glbl.nrth.dif <- factor(ifelse(data$`2.6` %in%
                                                 cntrs_euro,
                                               "Western Europe", 
                                               ifelse(data$`2.6` %in%
                                                        cntrs_nam,"United States",
                                                      ifelse(data$cntry.stdy_glbl.nrth.tri=="Latin America", "Latin America", NA))))

data%>%
  tab(cntry.stdy_glbl.nrth.dif)


data_temp <- data %>% 
  filter(cntry.stdy_glbl.nrth.dif=="Western Europe" | cntry.stdy_glbl.nrth.dif == "Latin America") %>%
  mutate(eur.la=factor(cntry.stdy_glbl.nrth.dif))

plot_subset(data=data_temp, by = ~ eur.la, by_name = "", out= c(),
            title = "",
            filename = "03_cntry.stdy_eur.lam")

# Fig 4

data_temp<-data%>%
  filter(`2.6`== "Estados Unidos" | cntry.stdy_glbl.nrth.tri == "Latin America") %>%
  mutate(usa.la = ifelse(`2.6`=="Estados Unidos", "United States","Latin America"))%>%
  mutate(usa.la=factor(usa.la))


plot1<-plot_subset_nosave(data=data_temp, by = ~ usa.la, by_name = "", out= c("Response Time", "Language", "Audience", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot2<-plot_subset_nosave(data=data_temp, by = ~ usa.la, by_name = "", out= c("Response Time", "Audience", "Acceptance Rate", "Language", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot3<-plot_subset_nosave(data=data_temp, by = ~ usa.la, by_name = "", out= c("Response Time", "Audience", "Acceptance Rate", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot4<-plot_subset_nosave(data=data_temp, by = ~ usa.la, by_name = "", out= c("Response Time", "Audience", "Acceptance Rate", "Editorial Location", "Scimago", "Methods", "Language", "Time Response"))
ggarrange(plot1, plot2, plot3, plot4, ncol=1, align="hv", common.legend = T, legend = "bottom" )
ggsave(filename = paste(path_plots, data_date, "03_cntr.stdy_us.lam.paper", type, sep = ""),
       width = 20, height = 16, units="cm")


# Figures 21, 22, 23
data_temp <- data %>%
  mutate(cname= `2.6`)%>%
  filter(cname=="Estados Unidos" | cname=="Chile"  |cname=="Alemania" | cname=="Países Bajos"
         |cname=="Argentina"|cname=="Brasil"|cname=="Colombia"|cname=="Peru"
         |cname=="Ecuador"|cname=="España"|cname=="Francia"|cname=="Italia"
         |cname=="México"|cname=="Reino Unido"|cname=="Suiza"|cname=="Uruguay")%>%
  mutate(cname.en=countryname(cname, "country.name.en"))%>%
  mutate(cname=as.factor(as.character(cname)))


#Figure 21
plot_subset_attribute(data=data_temp, by= ~cname, by_name="", 
                      attribute="Acceptance Rate",
                      title = "",
                      filename= "03_cntry.stdy.all_acceptance")

#Figure 22
plot_subset_attribute(data=data_temp, by= ~cname, by_name="", 
                      attribute="Editorial Location",
                      title = "",
                      filename= "03_cntry.stdy.all_location") 


#Figure 23
plot_subset_attribute(data=data_temp, by= ~cname, by_name="", 
                      attribute="Language", 
                      title = "",
                      filename= "03_cntry.stdy.all_language") 




# ---------------------- 6. Gender (Fig. 3) -------------------------------

data$gender <- data$`2.2`

data_temp <- data %>%
  filter(gender == "Femenino" | gender == "Masculino")
levels(data_temp$gender) <- list(Female = "Femenino", Male = "Masculino")

data_temp2 <- data %>%
  filter(cntry.stdy_glbl.nrth.dif=="Latin America")%>%
  filter(gender == "Femenino" | gender == "Masculino")

levels(data_temp2$gender) <- list(Female = "Femenino", Male = "Masculino")

# Fig 17
plot_subset(data = data_temp,  out= c(),
            by = ~ gender, by_name = "Gender",
            title = "",
            filename = "06_gndr")

# Fig 18
plot_subset(data = data_temp2,out= c(),
            by = ~ gender, by_name = "Gender",
            title = "",
            filename = "06_gndr_ctr.stdy.LA")

# Fig 3
plot1<-plot_subset_nosave(data=data_temp, by = ~ gender, by_name = "", out= c("Response Time", "Language", "Audience", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot2<-plot_subset_nosave(data=data_temp, by = ~ gender, by_name = "", out= c("Response Time", "Audience", "Acceptance Rate", "Language", "Scimago", "Methods", "Peer Pressure", "Time Response"))
ggarrange(plot1, plot2, ncol=1, align="hv", common.legend = T, legend = "bottom" )
ggsave(filename = paste(path_plots, data_date, "06_gender.paper", type, sep = ""),
       width = 20, height = 8, units="cm")


rm(data_temp,data_temp2); gc()





# ------------------------ 7. Theoretical Paradigm (Fig. 5) -----------------------

data_temp <- data

data$prdgm <- factor(case_when(data_temp$`3.4` == "Realismo" |
                                 data_temp$`3.4` == "Constructivismo"|
                                 data_temp$`3.4` == "Institucionalismo o Liberalismo" ~
                                 "Mainstream",
                               
                               data_temp$`3.4` == "Teoría Crítica" |
                                 data_temp$`3.4` == "Marxismo o Globalismo" |
                                 data_temp$`3.4` == "Poscolonialismo" |
                                 data_temp$`3.4` == "Post-estructuralismo" |
                                 data_temp$`3.4` == "Teoría Feminista" |
                                 data_temp$`3.4` == "Enfoques Latinoamericanos"  ~
                                 "Critical",TRUE ~ "None"))

data_temp <- data %>%
  filter(prdgm !="None") %>%
  mutate(prdgm = factor(prdgm), prdgm.list =factor(`3.4`))
levels(data_temp$prdgm) <- list(Mainstream = "Mainstream",
                                Critical = "Critical")

#Fig 24
plot_subset(data = data_temp,out=c(),
            by = ~ prdgm, by_name = "Paradigm",
            title = "",
            filename = "12_prdgm")


# Fig 5
plot1<-plot_subset_nosave(data=data_temp, by = ~ prdgm,  by_name = "", out= c("Response Time", "Language", "Acceptance Rate", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot2<-plot_subset_nosave(data=data_temp, by = ~ prdgm,  by_name = "", out= c("Response Time", "Audience", "Acceptance Rate", "Language", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot3<-plot_subset_nosave(data=data_temp, by = ~ prdgm,  by_name = "", out= c("Response Time", "Audience", "Acceptance Rate", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot4<-plot_subset_nosave(data=data_temp, by = ~ prdgm,  by_name = "", out= c("Audience", "Acceptance Rate", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Language"))

ggarrange(plot1, plot2, plot3,  plot4, ncol=1, align="hv", common.legend = T, legend = "bottom" )
ggsave(filename = paste(path_plots, data_date, "03_prdgm_paper", type, sep = ""),
       width = 20, height = 16, units="cm")




rm(data_temp); gc()









# --------------------------- 8. Area of Studies (Fig. 6) --------------------------

data$latam<-ifelse(is.na(data$`3.1_3`), "No", "Yes")
data$only_latam<-ifelse(is.na(data$`3.1_3`), "No", 
                        ifelse(is.na(data$`3.1_1`) &
                                 is.na(data$`3.1_2`) &
                                 is.na(data$`3.1_4`) &
                                 is.na(data$`3.1_5`) &
                                 is.na(data$`3.1_6`) &
                                 is.na(data$`3.1_7`) &
                                 is.na(data$`3.1_8`), "Yes","No"))

data_temp<-data%>%
  mutate(only_latam=factor(only_latam),
         latam=factor(latam))

# fig 25
plot_subset(data = data_temp,out= c(),
            by = ~ only_latam, by_name = "Area of Study: exclusively Latin America & Caribbean",
            title = "",
            filename = "10_area_onlyLA")

# Fig 6
plot1<-plot_subset_nosave(data=data_temp, by = ~ only_latam, by_name = "Area of Study: exclusively Latin America & Caribbean", out= c("Acceptance Rate", "Response Time", "Language", "Audience","Scimago", "Methods", "Peer Pressure", "Time Response"))
plot2<-plot_subset_nosave(data=data_temp, by = ~ only_latam, by_name = "Area of Study: exclusively Latin America & Caribbean", out= c("Response Time", "Audience", "Acceptance Rate", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Time Response"))
ggarrange(plot1, plot2, ncol=1, align="hv", common.legend = T, legend = "bottom" )
ggsave(filename = paste(path_plots, data_date, "10_area_onlyLA.paper", type, sep = ""),
       width = 20, height = 8, units="cm")


# ------------- 9. Importance of Publishing ( Fig 7) ------------

data %>%
  filter(check != 1, `2.8` == "Tiempo completo") %>%
  tab(`2.11`)

data$imprtnce <- ifelse(data$`2.11`=="Mucho","Mucho","Other")

data$imprtnce <- factor(data$imprtnce)
data$imprtnce2 <- ifelse(data$`2.11`=="Mucho"|data$`2.11`=="Algo","High","Low")

data$imprtnce2 <- factor(data$imprtnce2)

data_temp <- data

levels(data_temp$imprtnce) <- list(`Some/Not` = "Other", `A Lot` = "Mucho")
levels(data_temp$imprtnce2) <- list( `Low` = "Low",`High` = "High")

# Fig 27
plot_subset(data = data_temp,
            by = ~ imprtnce2, by_name = "Importance of Publishing",out=c(),
            title = "",
            filename = "10_imprtnce")

# Fig 7
plot1<-plot_subset_nosave(data=data_temp, by = ~ imprtnce2, by_name = "Importance of Publishing", out= c("Response Time", "Language", "Audience", "Editorial Location", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot2<-plot_subset_nosave(data=data_temp, by = ~ imprtnce2, by_name = "Importance of Publishing", out= c("Response Time", "Editorial Location", "Acceptance Rate", "Language", "Scimago", "Methods", "Peer Pressure", "Time Response"))
plot3<-plot_subset_nosave(data=data_temp, by = ~ imprtnce2, by_name = "Importance of Publishing", out= c("Response Time", "Audience", "Acceptance Rate", "Editorial Location", "Language", "Methods", "Peer Pressure", "Time Response"))
ggarrange(plot1, plot2, plot3,  ncol=1, align="hv", common.legend = T, legend = "bottom" )
ggsave(filename = paste(path_plots, data_date, "10_imprtnce.paper", type, sep = ""),
       width = 20, height = 12, units="cm")



rm(data_temp); gc()

# -------------------------- 10. Methods -------------------------

data$methods_interview <- data$`3.3`

data_temp <- data %>%
  filter(methods_interview %in% c("Ambos", "Cualitativos","Cuantitativos")) %>%
  mutate(methods_interview = factor(methods_interview),
         methods_quantitative = methods_interview)%>%
  filter(methods_quantitative!="Ambos")%>%
  mutate(methods_quantitative2= ifelse(methods_interview=="Cuantitativos", "Yes", "No"))%>%
  mutate(methods_quantitative2=factor(methods_quantitative2))


levels(data_temp$methods_interview) <- list(Mixed = "Ambos",
                                            Qualitative = "Cualitativos",
                                            Quantitative = "Cuantitativos")
levels(data_temp$methods_quantitative) <- list(
  Qualitative = "Cualitativos",
  Quantitative = "Cuantitativos")
levels(data_temp$methods_quantitative2) <- list(Quantitative = "Yes",
                                                Others = "No")

# Fig 26

plot_subset(data = data_temp, out= c(),
            by = ~ methods_quantitative, by_name = "Methods",
            title = "",
            filename = "11_mthds")



rm(data_temp); gc()

